// $Id: loggercounttransfer.cpp 40 2004-09-27 08:20:26Z hansemil $
#include "loggercounttransfer.h"
#include "loghandler.h"
#include "node.h"
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
using namespace std;

/**
 * @todo Write documentation
 */
LoggerCountTransfer::LoggerCountTransfer()
//	: LoggerCount()
{
	LogHandler::getInstance()->addLogger(this, "clock_tick");
}

/**
 * Opens file for writing and stores the Eos pointer
 */
void LoggerCountTransfer::eos_simulate_start(void *eos)
{
	openFile(&m_fout, "counttransfer.csv");
	m_fout << "Time Step, Average, Minimum, First Quartile, Median, Third Quartile, Maximum, Number of Nodes, Total Request Count" << endl;

	m_eos = (Eos *)eos;
}

/**
 * Closes file
 */
void LoggerCountTransfer::eos_simulate_stop(void *eos)
{
	m_fout.close();
}

/**
 * Gathers the statistics for each tick
 */
void LoggerCountTransfer::clock_tick(unsigned int time)
{
	int currentTransfers = 0, listSum = 0;
	std::vector<int> sizeList;

	// Write time step
	m_fout << time;

	// Get data
	std::list<Node *> *nodes = m_eos->getNodes();
	for (std::list<Node *>::iterator node=nodes->begin(); node!=nodes->end(); node++) {
		currentTransfers = (*node)->getTransferCount();
//m_fout << currentTransfers << endl;
		listSum += currentTransfers;
		sizeList.push_back(currentTransfers);
	}

	// Write statistics
	writeCountStats(m_fout, sizeList, listSum);
	m_fout << "," << listSum;
//m_fout << "-------------------------------------------------------------------------------" << endl;

	// Write end
	m_fout << endl;
}
